package com.tim.third.demo.printer.jb;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.tim.third.demo.printer.jb.config.Config;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

public class JbTest {

	// 添加打印机
	public void addDev(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		param.put("devName", "lzkj");// 设备名称。
//		param.put("grpID", "0");// 可选	int	分组ID

		send("adddev", param, 1);
	}

	// 删除打印机
	public void delDev(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);

		send("deldev", param, 1);
	}

	//查询打印机信息
	public void dev(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		send("device", param, 0);
	}

	//获取打印机状态
	public void getStatus(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey);
		param.put("securityCode", securityCode);

		send("getStatus", param, 0);
	}

	//修改打印机信息
	public void editDev(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		param.put("devName", "lzkj1");// 设备名称。
//		param.put("grpID", "1234");// 可选	int	分组ID

		send("editdev", param, 1);
		
	}

	//查询打印机列表
	public void listDev(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey);
		param.put("securityCode", securityCode);

		send("listDevice", param, 0);
		
	}

	//发送数据到打印机
	public void print() throws UnsupportedEncodingException, NoSuchAlgorithmException {

		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + Config.receiptDeviceNo + reqTime 
				+ Config.apiKey);
		param.put("securityCode", securityCode);
		
		/*
		 *打印信息的格式类型。取值范围[2-3]
		mode=2，自由格式打印（推荐使用）
		mode=3，十六进制命令集或十六进制字符串打印
		（无空格，如十六进制 0x12 0xAB 0xCD 0xEF，发送内容 12ABCDEF）
		 */
		param.put("mode", "2");
		param.put("msgDetail", "hello world<gpLogo/>\n");// 打印信息的内容

		send("sendMsg", param, 1);
		
	}

	//取消打印订单
	public void cancelPrint(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		param.put("all", "0");//是否全部清理，取值范围[0-1]1:是；0:否。默认 0 。
		send("cancelPrint", param, 1);
	}

	//设置票据打印机NVLogo
	public void setLogo(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		/*
		 	图片地址。
			设置LOGO 说明
			1、logo 图片地址需可访问，图片格式为jpg,png,gif，图片最佳效果是白底黑色图
			2、logo 打印结果可能与原图有出入
			3、图片宽度最大为280px
			4、设置成功后，mode=2 格式下设置打印内容包含<gpLogo/> 会打印LOGO。
			5、只适用于票据打印机。设置成功打印机会发出一声“滴”
		 */
		param.put("imgUrl", "https://online-lazypig.lazipig.com/headicon.png");
		send("setLogo", param, 1);

	}

	//删除票据打印机设置的NVLogo（只适用于票据打印机）。
	public void deleteLogo(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);

		send("deleteLogo", param, 1);

	}

	//打印机音量设置
	public void sendVolume(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);

		param.put("volume", "75");//音量大小。取值范围 0-100，建议(0,25,50,75, 85,100)
		send("sendVolume", param, 1);

	}

	//设置指定打印机播放的语音类型（只适用于内置喇叭的打印机）。
	public void setVoiceType(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey + Config.receiptDeviceNo);
		param.put("securityCode", securityCode);
		
		//语音类型。0 真人语音，1 滴滴语音，2 不自动播放新订单，真人语音，3 不自动播放新订单，滴滴语音。
		param.put("voiceType", "1");
		send("setVoiceType", param, 1);

	}

	//查询账户下所有的模板信息
	public void listTemplate(){
		Map<String, Object> param = new HashMap<>();
		String reqTime = String.valueOf(Calendar.getInstance().getTimeInMillis());
		param.put("reqTime", reqTime);
		String securityCode = DigestUtils.md5Hex(Config.memberCode + reqTime 
				+ Config.apiKey);
		param.put("securityCode", securityCode);

		send("listTemplate", param, 1);

	}

	//指定模板编号与模板参数，发送数据到 WEB API 并通过打印机打印出来
	public void templetPrint(){
		Map<String, Object> param = new HashMap<>();
		param.put("templetID", "");// 模板编号
		/*   
		 模板参数列表，JSON格式字符串，JSON格式中所有参数key与value皆为字符，key不可为空，value允许为空。
			单个订单模式：
			{"orderid":"8000212345677001","goodsname":"测试货物","date":"2018-06-06","remarks":""}

			多订单模式：
			{"ordernum001":{"orderid": "8000212345677001", "goodsname": "测试货物", "date": "2018-06-06", "remarks": ""},"ordernum002":{"orderid": "8000212345677001", "goodsname": "测试货物", "date": "2018-06-06", "remarks": ""},"ordernum003":{"orderid": "8000212345677001", "goodsname": "测试货物", "date": "2018-06-06", "remarks": ""}}
		 */       
		param.put("tData", "");
		send("templetPrint", param, 1);

	}

	public void send(String urlSuffix, Map<String, Object> param, int isPost){
		String url = "https://api.poscom.cn/apisc/"+urlSuffix;

		param.put("memberCode", Config.memberCode);
		param.put("deviceID", Config.receiptDeviceNo);
		
		System.out.println(JSONUtil.formatJsonStr(url));
		System.out.println(JSONUtil.formatJsonStr(JSON.toJSONString(param)));
		String post = isPost==1 ? HttpUtil.post(url, getUrlEncoded(param))
					: HttpUtil.get(url, param);
		System.out.println("\n请求结果：");
		System.out.println(JSONUtil.formatJsonStr(post));
	}
	
	public String getUrlEncoded(Map<String, Object> param){
		StringBuilder builder = new StringBuilder();
		for(String key : param.keySet()){
			builder.append("&").append(key)
				.append("=").append(param.get(key));
		}
		return builder.substring(1, builder.length());
	}
}
